
#include <mem.h>

/* ------------------------------------------------------------------------- */

static uint8 rom_buf[ROM_FIX];

/* ------------------------------------------------------------------------- */

void fastcall rom_init(const void *image)
{
	memcpy(rom_buf, image, ROM_FIX);
}

uint32 fastcall rom_read(uint32 addr)
{
	size_t size;
	uint32 data;

	if (addr >= ROM_SIZE)
		return 0xffffffff;

	if (addr > ROM_SIZE - 4) {
		data = 0xffffffff;
		addr &= ROM_FIX - 1;
		memcpy(&data, &rom_buf[addr], ROM_FIX - addr);
		return data;
	}

	addr &= ROM_FIX - 1;

	if (addr <= ROM_FIX - 4)
		return *(uint32 *)(&rom_buf[addr]);

	/* loop access */
	data = 0xffffffff;
	size = ROM_FIX - addr;
	memcpy(&data, &rom_buf[addr], size);
	memcpy((uint8 *)(&data) + size, rom_buf, 4 - size);
	return data;
}

/* ------------------------------------------------------------------------- */
